<?php
if (isset($_COOKIE["cur_user"]) and isset($_GET["courseID"])) {
    $conn = new PDO("sqlite:storage.db");
    $name = $_COOKIE["cur_user"];
    $cid = $_GET["courseID"];
    $sql_command = <<<EOF
insert into choose
select user.sid, ? from user where name=?
EOF;
    $sql_run = $conn->prepare($sql_command);

    if ($sql_run->execute([$cid, $name])) {
        $sql_run->closeCursor();
        echo <<<EOF
<script>
alert("Registration successful!");
window.location.href = "stuCourseSelected.php";
</script>
EOF;
    }
} else {
    echo <<<EOF
<script>
alert("You need to log in!");
window.location.href = "index.php";
</script>
EOF;

}
